package edu.action;



import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;

import edu.model.Role;
import edu.model.User;
import edu.model.UserRole;
import edu.service.RoleService;
import edu.service.UserRoleService;
import edu.service.UserService;


public class UserAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2023260004366329501L;
	/**
	 * 
	 */
	private int	userId = 0;
	private int	roleId = 0;
	private String	userName;
	private String	userPassword;
	private String	oldUserPassword;
	private User user = null;
	private List<User> userList = null;
	private UserService userService = null;
	private RoleService roleService = null;
	private UserRoleService userRoleService = null;
	
	
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public int getRoleId() {
		return roleId;
	}
	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPassword() {
		return userPassword;
	}
	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}
	public void setOldUserPassword(String oldUserPassword) {
		this.oldUserPassword = oldUserPassword;
	}
	public String getOldUserPassword() {
		return oldUserPassword;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public List<User> getUserList() {
		return userList;
	}
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	public RoleService getRoleService() {
		return roleService;
	}
	public UserRoleService getUserRoleService() {
		return userRoleService;
	}
	public void setUserRoleService(UserRoleService userRoleService) {
		this.userRoleService = userRoleService;
	}
	
	public String list(){
		userList = userService.getAllUserOderBy("userId", "desc");
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String add(){
		
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(userPassword.getBytes());
			
			userPassword = new BigInteger(1, md.digest()).toString(16);

			User user = new User();
			user.setUserName(userName);
			user.setUserPassword(userPassword);
			Role role = roleService.getRoleById(roleId);
			
			UserRole userRole = new UserRole();
			userRole.setRole(role);
			userRole.setUser(user);
			
			user.getUserRoles().add(userRole);
			
			userService.saveUser(user);
			userId = user.getUserId();
			return SUCCESS;
		} catch (NoSuchAlgorithmException e) {	
			e.printStackTrace();
		}
		return ERROR;
	}
	
	public String editPwd(){
		try {
			User user = userService.getUserById(userId);
			if(user != null) {
				MessageDigest md = MessageDigest.getInstance("MD5");		
				md.update(oldUserPassword.getBytes());
				oldUserPassword = new BigInteger(1, md.digest()).toString(16);
			
				if(user.getUserPassword().equals(oldUserPassword)) {
					md.update(userPassword.getBytes());
					userPassword = new BigInteger(1, md.digest()).toString(16);
				
					user.setUserPassword(userPassword);
					userService.updateUser(user);
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}
	
	public String delete(){	
		User user = userService.getUserById(userId);
		if(user != null) {
			Set<?> userRoles = user.getUserRoles();
			
			for(Iterator<?> i= userRoles.iterator(); i.hasNext() ; ) {
				UserRole userRole = (UserRole) i.next();
				userRoleService.deleteUserRole(userRole);
			}
			userService.deleteUser(user);
			return SUCCESS;
		}
		return ERROR;
	}
	
}
